package ro.myApp.security.client;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.security.Identity;

import ro.myApp.client.BaseView;
import ro.myApp.security.dto.UserCreateMessage;
import ro.myApp.security.dto.UserRegistrationMessage;
import ro.myApp.security.entities.User;
import ro.myApp.security.util.RoleEnum;

@Name("register")
@BypassInterceptors
@Scope(ScopeType.PAGE)
public class Register extends BaseView {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4215954318191612680L;
	
	private User instance;
	
	List<RoleEnum> selectedInterests = new LinkedList<RoleEnum>();
	
	public User getInstance() {
		if (null==instance)
			instance = sendMessage(new UserCreateMessage());
		return instance;
	}
	
	public boolean register (){
		instance.setInterests(selectedInterests.toString());
		Object result = sendMessage(new UserRegistrationMessage(instance));
		//the mailing service has no return result - null means successful sent mail
		if (null==result){
			Identity.instance().getCredentials().setPassword(instance.getPassword());
			return true;
		}
		return false;
	}
	
	public List<RoleEnum> getAllInterests() {
		return Arrays.asList(RoleEnum.values());
	}
	
	public List<RoleEnum> getSelectedInterests() {
		return selectedInterests;
	}
	
	public void setSelectedInterests(List<RoleEnum> selectedInterests) {
		this.selectedInterests = selectedInterests;
	}
}
